<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="t('脚本配置')"
    @ok="handleSubmit"
    @visible-change="visibleChange"
    width="70%"
  >
    <div class="config-box">
      <div>
        <div class="box-top">
          <div class="title">{{ t('脚本示例') }}</div>
          <a-button type="primary" size="small" @click="componentVisible = true">{{
            t('选择组件')
          }}</a-button>
        </div>
        <div>
          <div
            v-for="(item, index) in example"
            :key="index"
            :class="['script-name', { 'active-name': activeIndex === index }]"
            @click="activeIndex = index"
          >
            {{ item.title }}
          </div>
        </div>
      </div>
      <div style="max-width: 500px">
        <div class="box-top">
          <div class="title">{{ t('自定义脚本') }}</div>
        </div>
        <CodeEditor v-model:value="definedScript" language="js" />
      </div>
      <div>
        <div class="box-top">
          <div class="title">{{ t('脚本示例代码') }}</div>
        </div>
        <CodeEditor :value="example[activeIndex].code" language="js" readonly />
      </div>
    </div>
    <a-drawer
      :title="t('组件列表(可将组件拖入自定义脚本当中)')"
      placement="left"
      :visible="componentVisible"
      :get-container="false"
      :style="{ position: 'absolute' }"
      :mask="false"
      width="50%"
      :closable="false"
    >
      <Draggable
        v-model="widgetFormList"
        :group="{ name: 'script', pull: false, put: false }"
        item-key="key"
        @end="dragEnd"
        :sort="false"
      >
        <template #item="{ element }">
          <a-tag color="blue">{{ `${element.label}(${element.bindField})` }}</a-tag>
        </template>
      </Draggable>
      <template #extra>
        <CloseOutlined @click="componentVisible = false" />
      </template>
    </a-drawer>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, reactive, inject } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { CodeEditor } from '/@/components/CodeEditor';
  import Draggable from 'vuedraggable';
  import { CloseOutlined } from '@ant-design/icons-vue';
  import { camelCaseString } from '/@/utils/event/design';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const eventType = ref('');
  const definedScript = ref(``);
  const emit = defineEmits(['success', 'register']);
  const activeIndex = ref(0);
  const componentVisible = ref<boolean>(false);

  const widgetFormList = ref([]); //整个表单json配置
  const isCustomForm = inject<boolean>('isCustomForm', false);
  const example = reactive([
    {
      title: t('读写组件值'),
      code: `const value = formModel.bindField //获取组件值
  formModel.bindField = 1 //修改组件值

  // bindField: 组件的绑定字段`,
    },
    {
      title: t('ajax加载服务器数据'),
      code: `formActionType.httpRequest({
      requestType: 'get', //请求方式有: get、post、put、delete
      requestUrl: '/system/dictionary-detail', //请求地址
      params: {
        itemId: '1419276800524423168'
      },//请求参数
      errorMessageMode: 'none' //错误提示方式，默认为none
  })
        // errorMessageMode='message' 错误提示为消息提示
        // errorMessageMode='modal' 显示modal错误弹窗，用于一些比较重要的错误
        // errorMessageMode='none'  一般是调用时明确表示不希望自动弹出错误提示
    `,
    },
    {
      title: t('修改组件样式'),
      code: `formActionType.changeStyle(schema, { border:'2px solid #5e95ff' },'bindField')

  // schema:必须要写
  // 中间对象为需要修改的样式
  // bindField:需要修改组件的绑定字段（字符串），如修改本组件 则不填`,
    },
    {
      title: t('弹出对话框'),
      code: `formActionType.showModal({
      title: '我是标题', // 对话框标题
      content: '我是内容', // 对话框内容
      onOk() {
        console.log('onOk') // 点击确定按钮的处理逻辑
      },
      onCancel() {
        console.log('onCancel') // 点击取消按钮的处理逻辑
      },
  });`,
    },
    {
      title: t('if判断'),
      code: `const value = 3
  if(value === 3){ /* 括号内为判断条件 */
    console.log('value为3')
    /* 处理逻辑 */
  }`,
    },
    {
      title: t('循环'),
      code: `for(let i=0; i<3; i++){ /* 括号内为循环条件 */
    console.log('i的值为', i)
    /* 处理逻辑 */
  }`,
    },
    {
      title: t('正则校验'),
      code: `formActionType.regTest({
      regExpression: /^[0-9]/,// 正则表达式
      testValue: 123,// 需要进行校验的值
      successMessage: '校验成功',// 校验成功的提示信息
      errorMessage: '校验失败'// 校验失败的提示信息
  })`,
    },
    {
      title: t('刷新API'),
      code: `formActionType.refreshAPI('bindField')

  // bindField:需要刷新API组件的绑定字段（字符串）`,
    },
  ]);
  const [registerModal, { closeModal, setModalProps }] = useModalInner(async (data) => {
    widgetFormList.value = data.list;
    eventType.value = data.type;
    //如果已经设置过当前事件代码  则显示已经设置的值 反之  显示默认值
    definedScript.value = data.content;
    setModalProps({
      fixedHeight: true,
    });
  });

  const dragEnd = ({ item }) => {
    const component = item._underlying_vm_;
    const str = `${component.label}(${component.bindField})`;
    const replaceStr = isCustomForm
      ? component.bindField
      : `${camelCaseString(component.bindField)}`;
    definedScript.value = definedScript.value.replace(str, replaceStr);
  };
  const handleSubmit = () => {
    emit('success', eventType.value, definedScript.value);
    closeModal();
  };

  const visibleChange = (visible: boolean) => {
    if (!visible) componentVisible.value = false;
  };
</script>

<style lang="less" scoped>
  .config-box {
    display: flex;
    height: 100%;

    div:nth-child(1) {
      flex: 1;
    }

    div {
      flex: 2;
    }

    .box-top {
      display: flex;
      justify-content: space-between;
      height: 30px;
      border-bottom: 1px solid #f0f0f0;
      margin: 0 0 10px 10px;
    }

    .script-name {
      cursor: pointer;
      padding: 10px 0 10px 15px;

      &:hover {
        background: #eef4ff;
      }
    }

    .active-name {
      background: #eef4ff;
    }
  }

  :deep(.ant-drawer-title),
  .title {
    font-size: 16px;
    height: 20px;
    line-height: 18px;
    padding-left: 6px;
    border-left: 6px solid #5e95ff;
  }

  :deep(.ant-tag) {
    padding: 8px;
    font-size: 13px;
    margin-bottom: 7px;
    cursor: move;
  }
</style>
